function h = jcb_plot_lumerical_profile(filename,type,savefigure,fileformat,dim)

%% initial setup
if nargin<5
    dim='';
end
if nargin<4
    fileformat='png';
end
if nargin<3
    savefigure=false;
end
if nargin<2
    error('not enough input arguments');
end

if strcmpi(dim,'x')
    component='Ex';
elseif strcmpi(dim,'y')
    component='Ey';
elseif strcmpi(dim,'z')
    component='Ez';
else
    component='|E|^2';
end

[pathname filename ext] = fileparts(filename);

disp(['            creating figure for ' filename ext]);
profile = load(fullfile(pathname,[filename ext]));
x=profile(2:end,1);
y=profile(1,2:end);
offsetx=diff(x)/2;
offsety=diff(y)/2;
offsetx=offsetx([1:end end]);
offsety=offsety([1:end end]);
profile=profile(2:end,2:end);

f=fopen(fullfile(pathname,[filename ext]));
try
    textline = fgetl(f);
    tokens=regexp(textline,'lambda=([\d\.e\+]*).* ([^ .]*)','tokens');
    lambda=tokens{1}{1};
    fspfile=tokens{1}{2};
    textline = fgetl(f);
    tokens=regexp(textline,'(\w)=(\d*\.?\d*)','tokens');
    idleaxisname=tokens{1}{1};
    idleaxis=tokens{1}{2};
    axisnames='xyz';
    workingaxes=upper(axisnames(axisnames~=idleaxisname));
catch E
    disp(textline);
    fclose(f);
    throw(E);
end
fclose(f);
%% plot profile
h = figure(63609);
clf;

%caxis([log10(min(profile(:))), log10(max(profile(:)))]);

if strcmpi(type,'nearfield')
    if strcmp(component,'|E|^2')
        jcb_contourplot(x-offsetx,y-offsety,log10(profile));
        title(['log(|E|^2) profile at \lambda=' lambda 'nm at ' idleaxisname '=' idleaxis 'nm for ' strrep(fspfile,'_','\_') '.fsp']);
    else
        jcb_contourplot(x-offsetx,y-offsety,profile);
        title([component ' profile at \lambda=' lambda 'nm at ' idleaxisname '=' idleaxis 'nm for ' strrep(fspfile,'_','\_') '.fsp']);
    end
else
    jcb_contourplot(x-offsetx,y-offsety,profile);
    title(['n profile at \lambda=' lambda 'nm at ' idleaxisname '=' idleaxis 'nm for ' strrep(fspfile,'_','\_') '.fsp']);
    caxis([-1 4]);
end
xlabel([workingaxes(1) '/nm']);
ylabel([workingaxes(2) '/nm']);

%% save figure to file
if savefigure
    print('-zbuffer',['-d' fileformat],fullfile(pathname,strrep(filename,'.txt','')));
end

close(63609);

if nargout < 1
    clear h
end